{include file='Public/header' /}
<div class="layui-layout layui-layout-admin">
{include file='Public/head' /}	
{include file='Public/side' /}
<div class="layui-body"> 
{include file='Public/crumbs' /}
	<!-- 内容主体区域 -->
	<div class="shu-body">
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12 layui-col-xs12">
					<form id="export-form" class="layui-form" method="post" action="{:url('export')}">
						<table lay-even class="layui-table">
							<colgroup>
							<col width="50">
							</colgroup>
							<thead>
								<tr>
									<th> 
									<input type="checkbox" name="checkAll" lay-filter="checkAll">
									</th>
									<th>表名</th>
                                    <th>数据量</th>
                                    <th>数据大小</th>
                                    <th>创建时间</th>
                                    <th>备份状态</th>
                                    <th>操作</th>
								</tr>
							</thead>
							<tbody>
							{volist name="list" id="table"}
							<tr>
								<td align="center"> 
								<input type="checkbox" class="ids" name="tables[{$table['id']}]" lay-filter="checkOne" value="{$table['name']}">
								</td>
								<td>{$table.name}</td>
                                <td>{$table.rows}</td>
                                <td>{$table.data_length|format_bytes}</td>
                                <td>{$table.create_time}</td>
								<td class="info">未备份</td>
								<td>
								<a class="layui-btn layui-btn-small yhb_btn"  href="javascript:;" nickname="{$table['name']}" val="{:url('Database/optimize?tables='.$table['name'])}">优化表</a>
								 
								<a class="layui-btn layui-btn-small layui-btn-danger xfb_btn"  href="javascript:;" nickname="{$table['name']}" val="{:url('Database/repair?tables='.$table['name'])}">修复表</a> 
								 
								</td>
							</tr>
							{/volist}
							</tbody>
						</table>
						<a id="export" class="layui-btn layui-btn-danger" lay-submit lay-filter="bf">立即备份</a>
						<a class="layui-btn layui-btn-small" lay-submit lay-filter="yhb"  href="javascript:;" nickname="{$table['name']}" val="{:url('Database/optimize')}">优化表</a>
						<a class="layui-btn layui-btn-small" lay-submit lay-filter="xhb"  href="javascript:;" nickname="{$table['name']}" val="{:url('Database/repair')}">修复表</a>
						<a href="{:url('Database/recovery')}" class="layui-btn layui-btn-danger" lay-submit>数据还原</a>
					</form>
				</div>
			</div>
		</div>
	</div>
	<!-- 内容主体区域 END--> 
</div>
{include file='Public/footer' /} 
<script>
layui.use('form',function(){
  var form = layui.form,
  jq = layui.jquery,
  $ = layui.$;
  
  form.on('checkbox(checkAll)', function(data){
    if(data.elem.checked){
      jq("input[lay-filter='checkOne']").prop('checked',true);
    }else{
      jq("input[lay-filter='checkOne']").prop('checked',false);
    }
    form.render('checkbox');
  });  

  form.on('checkbox(checkOne)', function(data){
    var is_check = true;
    if(data.elem.checked){
      jq("input[lay-filter='checkOne']").each(function(){
        if(!jq(this).prop('checked')){ is_check = false; }
      });
      if(is_check){
        jq("input[lay-filter='checkAll']").prop('checked',true);
      }
    }else{
      jq("input[lay-filter='checkAll']").prop('checked',false);
    } 
    form.render('checkbox');
  });

  jq('.yhb_btn').click(function(){
	var name = jq(this).attr('nickname');
    var url = jq(this).attr('val');
    layer.confirm('确定优化【'+name+'】表?', function(index){
      loading = layer.load(2, {
        shade: [0.2,'#000']
      });
      jq.get(url,function(data){
        if(data.code == 1){
          layer.close(loading);
          layer.msg(data.msg, {icon: 1, time: 1000}, function(){
            location.reload();
          });
        }else if(data.code == 0){
          layer.close(loading);
          layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
        }
      });
    });   
  });
     
  jq('.xfb_btn').click(function(){
	var name = jq(this).attr('nickname');
    var url = jq(this).attr('val');
    layer.confirm('确定修复【'+name+'】表?', function(index){
      loading = layer.load(2, {
        shade: [0.2,'#000']
      });
      jq.get(url,function(data){
        if(data.code == 1){
          layer.close(loading);
          layer.msg(data.msg, {icon: 1, time: 1000}, function(){
            location.reload();
          });
        }else if(data.code == 0){
          layer.close(loading);
          layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
        }
      });
    });   
  });

  //优化表选择	
  form.on('submit(yhb)', function(data){
    var is_check = false;
    jq("input[lay-filter='checkOne']").each(function(){
      if(jq(this).prop('checked')){ is_check = true; }
    });
    if(!is_check){
      layer.msg('请选择数据', {icon: 2,anim: 6,time: 1000});
      return false;
    }
    layer.confirm('确定批量优化?', function(index){
      loading = layer.load(2, {
        shade: [0.2,'#000']
      });
      var param = data.field;
      jq.post("{:url('Database/optimize')}",param,function(data){
        if(data.code == 1){
          layer.close(loading);
          layer.msg(data.msg, {icon: 1, time: 1000}, function(){
            location.reload();
          });
        }else if(data.code == 0){
          layer.close(loading);
          layer.msg(data.msg, {icon: 2,anim: 6, time: 1000});
        }
      });
    });
	
    return false;
  });
  
  //修复表选择	
  form.on('submit(xhb)', function(data){
    var is_check = false;
    jq("input[lay-filter='checkOne']").each(function(){
      if(jq(this).prop('checked')){ is_check = true; }
    });
    if(!is_check){
      layer.msg('请选择数据', {icon: 2,anim: 6,time: 1000});
      return false;
    }
    layer.confirm('确定批量修复?', function(index){
      loading = layer.load(2, {
        shade: [0.2,'#000']
      });
      var param = data.field;
      jq.post("{:url('Database/repair')}",param,function(data){
        if(data.code == 1){
          layer.close(loading);
          layer.msg(data.msg, {icon: 1, time: 1000}, function(){
            location.reload();
          });
        }else if(data.code == 0){
          layer.close(loading);
          layer.msg(data.msg, {icon: 2,anim: 6, time: 1000});
        }
      });
    });
	
    return false;
  });
  
    
})
</script> 
<script type="text/javascript">
////////////////
    (function ($) {
        var $form = $("#export-form"), $export = $("#export"), tables
        $optimize = $("#optimize"), $repair = $("#repair");
		////////暂不用
        $optimize.add($repair).click(function () {
            $.post(this.href, $form.serialize(), function (data) {
				//console.log(data);
				if(data.code == 1){
				  layer.close(loading);
				  layer.msg(data.msg, {icon: 1, time: 1000}, function(){
					location.reload();
				  });
				}else if(data.code == 0){
				  layer.close(loading);
				  layer.msg(data.msg, {icon: 2,anim: 6, time: 1000});
				}
                setTimeout(function () {
                    $('#top-alert').find('button').click();
                    $(this).removeClass('disabled').prop('disabled', false);
                }, 1500);
            }, "json");
            return false;
        });
		////////////
        $export.click(function () {
            $export.parent().children().addClass("disabled");
            $export.html("正在发送备份请求...");
            $.post(
                    $form.attr("action"),
                    $form.serialize(),
                    function (data) {
                        if (data.code) {
                            tables = data.data.tables;
                            $export.html(data.msg + "开始备份，请不要关闭本页面！");
                            backup(data.data.tab);
                            window.onbeforeunload = function () {
                                return "正在备份数据库，请不要关闭！"
                            }
                        } else {
                            alert(data.msg, 'alert-error');
                            $export.parent().children().removeClass("disabled");
                            $export.html("立即备份");
                            setTimeout(function () {
                                $('#top-alert').find('button').click();
                                $(that).removeClass('disabled').prop('disabled', false);
                            }, 1500);
                        }
                    },
                    "json"
            );
            return false;
        });

        function backup(tab, code) {
            code && showmsg(tab.id, "开始备份...(0%)");
            $.get($form.attr("action"), tab, function (data) {
				//console.log(data);
                if (data.code) {
                    showmsg(tab.id, data.msg);

                    if (!$.isPlainObject(data.data.tab)) {
                        $export.parent().children().removeClass("disabled");
                        $export.html("备份完成，点击重新备份");
                        window.onbeforeunload = function () {
                            return null
                        }
                        return;
                    }
                    backup(data.data.tab, tab.id != data.data.tab.id);
                } else {
                    alert(data.msg, 'alert-error');
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function () {
                        $('#top-alert').find('button').click();
                        $(that).removeClass('disabled').prop('disabled', false);
                    }, 1500);
                }
            }, "json");

        }

        function showmsg(id, msg) {
            $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
        }
    })(jQuery);
///////////////
</script>
